package com.config;

import com.config.properties.LoginFreeProperties;
import com.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: xufan
 * @Description 拦截切面注册
 **/
@Configuration
@EnableConfigurationProperties(LoginFreeProperties.class)
public class WebAppConfigurer implements WebMvcConfigurer {
    @Autowired
    private LoginFreeProperties loginFreeProperties;

    /**
     * @author: xufan
     * @Date: 2021/8/5
     * @Description: 因为拦截器在上下文加载之前，所以要手动注册洗拦截器，这样才能使注入生效
     */
    @Bean
    public AuthInterceptor getAuthInterceptor() {
        return new AuthInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //免登录的路由
        String[] listRoutesNoToken = loginFreeProperties.getRoutes();
        //对非免登录的路由进行拦截（用户token校验拦截）
        registry.addInterceptor(getAuthInterceptor()).addPathPatterns("/**").excludePathPatterns(listRoutesNoToken);
    }
}
